import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author YuanYuan
 * @Date 2022/11/21
 * @Time 21:53
 */
@WebServlet("/message")
public class MessageWall extends HttpServlet {

    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Message message = objectMapper.readValue(req.getInputStream(),Message.class);
        try {
            save(message);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        resp.setStatus(200);
        System.out.println("提交数据成功: from=" + message.getFrom()
                + ", to=" + message.getTo() + ", message=" + message.getMessage());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        try {
            resp.getWriter().write(objectMapper.writeValueAsString(load()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private List<Message> load() throws SQLException {
        //先有一个数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/2022test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");
        //建立连接
        Connection connection = (Connection) dataSource.getConnection();
        //构造SQL
        String sql = "select * from message";
        PreparedStatement statement = connection.clientPrepareStatement(sql);
        //执行SQL
        ResultSet resultSet = statement.executeQuery();
        //遍历结果集合
        List<Message> messageList = new ArrayList<>();
        while(resultSet.next()) {
            Message message = new Message();
            message.setFrom(resultSet.getString("from"));
            message.setTo(resultSet.getString("to"));
            message.setMessage(resultSet.getString("message"));
            messageList.add(message);
        }
        //关闭连接
        statement.close();
        connection.close();

        return messageList;

    }

    private void save(Message message) throws SQLException {
        //先有一个数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/2022test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");
        //建立连接
        Connection connection = (Connection) dataSource.getConnection();
        //构造sql
        String sql = "insert into message value(?,?,?)";
        PreparedStatement statement = connection.clientPrepareStatement(sql);
        statement.setString(1,message.getFrom());
        statement.setString(2,message.getTo());
        statement.setString(3,message.getMessage());
        //执行sql
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);

        //关闭连接
        statement.close();
        connection.close();
    }
}
